package com.weixing.mall.provider.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.base.enums.IsEnableEnum;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.provider.mapper.DepotKeeperMapper;
import com.weixing.mall.provider.mapper.DepotMapper;
import com.weixing.mall.provider.model.domain.Depot;
import com.weixing.mall.provider.model.domain.DepotKeeper;
import com.weixing.mall.provider.model.dto.DepotDto;
import com.weixing.mall.provider.model.query.DepotQuery;
import com.weixing.mall.provider.model.vo.DepotKeeperVo;
import com.weixing.mall.provider.model.vo.DepotVo;
import com.weixing.mall.provider.service.IDepotService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author wx
 * @Date 2019/10/22 19:07
 * @Version 1.0
 */
/**
 * <p>
 * 仓库模块-仓库表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-15
 */
@Service
public class DepotServiceImpl extends ServiceImpl<DepotMapper, Depot> implements IDepotService {
    @Autowired
    private DepotMapper depotMapper;
    @Autowired
    private DepotKeeperMapper depotKeeperMapper;


    @Override
    public List<DepotVo> selectDepotKeeperList() {
        List<DepotVo> depotVos = depotMapper.selectDepotKeeperList();
        for (DepotVo depotVo : depotVos) {
            List<DepotKeeperVo> kepperList=depotKeeperMapper.selectByDepotId(depotVo.getId());
            depotVo.setDepotAdmin(kepperList);
        }
        return depotVos;
    }
    @Override
    public List<DepotVo> listBySysId(Long uid,Integer flag) {
        List<DepotVo> depotVos = depotMapper.listBySysId(uid,flag);
        return depotVos;
    }

    @Override
    public IPage<DepotVo>selectList(Page page, DepotQuery depotQuery) {
        return depotMapper.selectByList(page,depotQuery);
    }

    @Override
    public void create(DepotDto depotDto) {
        Depot depot = new Depot();
        BeanUtils.copyProperties(depotDto,depot);
        depot.setIsEnable(depotDto.getIsEnable().getValue());
        depot.setIsDefective(depotDto.getIsDefective().getValue());
        depot.setIsCommodity(depotDto.getIsCommodity().getValue());
        depot.setCreatedTime(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        if(depotDto.getId()==null){
            depotMapper.insert(depot);
        }else {
            depotMapper.updateById(depot);
        }
        depotKeeperMapper.deleteByDepotId(depot.getId());
        Integer[] depotAdmin = depotDto.getDepotAdmin();
        DepotKeeper depotKeeper = new DepotKeeper();
        if(depotAdmin.length>0){
            for (Integer admin : depotAdmin) {
                depotKeeper.setKeeper(Long.valueOf(admin));
                depotKeeper.setDepotId(depot.getId());
                depotKeeperMapper.insert(depotKeeper);
            }
        }
    }

    @Override
    public void updateStatus(long id, IsEnableEnum isEnable) {
        depotMapper.updateStatus(id,isEnable);
    }

    @Override
    public Depot getDepotByCode(String code){
        return getOne(new QueryWrapper<Depot>().eq("depot_code", code).eq("is_enable", WhetherEnum.YES.getValue()));
    }


}
